package cn.edu.zhku.controller;

import cn.edu.zhku.aspect.annotation.*;
import cn.edu.zhku.aspect.enmu.DataType;
import cn.edu.zhku.constant.ResponseCode;
import cn.edu.zhku.dto.GroupDto;
import cn.edu.zhku.dto.TaskDto;
import cn.edu.zhku.pojo.Group;
import cn.edu.zhku.pojo.Task;
import cn.edu.zhku.service.TaskService;
import cn.edu.zhku.swagger.notes.TaskApiNotes;
import cn.edu.zhku.util.PojoUtil;
import cn.edu.zhku.util.ResultUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequestMapping("/api/task")
@RestController
public class TaskController {

    @Autowired
    private TaskService taskService;

    @RequiresPermissions("task:get")
    @CheckDataExist("没有查到任务相关信息")
    @GetMapping("/{taskId}")
    @ApiOperation("获取任务的基本信息")
    public JSONObject getTask(@PathVariable Integer taskId) {
        Task task = this.taskService.findById(taskId);
        return ResultUtil.getResult(ResponseCode.ok, "成功返回任务的基本信息", task);
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:add")
    @SetUserId
    @PostMapping("/add")
    @ApiOperation(value = "教师端添加任务", notes = TaskApiNotes.addTask)
    public JSONObject addTask(@RequestBody TaskDto taskDto) {
        Task task = taskDto.getTask();
        task.setCreateTime(new Date());
        this.taskService.addOne(task);
        return ResultUtil.getResult(ResponseCode.ok, "创建任务成功");
    }

    @RequiresRoles("teacher")
    @SetUserId
    @CheckTaskExist("任务不存在")
    @CheckDataExist("当前无可分发的群组")
    @Paging(DataType.GROUP)
    @PostMapping("/distAble")
    @ApiOperation(value = "教师端获取某个任务可分发的群组列表", notes = TaskApiNotes.getGroupDistAble)
    public JSONObject getGroupDistAble(@RequestBody GroupDto groupDto) {
        PageHelper.startPage(groupDto.getPageNum(), groupDto.getPageSize());
        List<Group> groups = this.taskService.findGroupNotInTaskTea(groupDto);
        PageInfo<Group> pageInfo = new PageInfo<>(groups);
        return ResultUtil.getResult(ResponseCode.ok, "成功返回群组列表", pageInfo);
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:dist")
    @CheckTaskExist("任务不存在")
    @CheckGroupExist(value = "所选（部分）群组不存在", batch = true)
    @PostMapping("/distBatch")
    @ApiOperation(value = "教师端分发任务到多个群组", notes = TaskApiNotes.distTaskBatch)
    public JSONObject distTaskBatch(@RequestBody TaskDto taskDto) {
        Integer taskId = taskDto.getTaskId();
        List<Integer> groupIds = taskDto.getGroupIds();
        if (!this.taskService.checkTaskGroupNotExistByTaskIdAndGroupIdBatch(taskId, groupIds))
            return ResultUtil.getResult(ResponseCode.conflict, "请勿重复分发任务");
        this.taskService.distTaskBatch(taskId, groupIds);
        return ResultUtil.getResult(ResponseCode.ok, "分发任务成功");
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:edit")
    @CheckTaskExist("任务不存在")
    @PatchMapping("/edit")
    @ApiOperation(value = "教师修改任务", notes = TaskApiNotes.editTask)
    public JSONObject editTask(@RequestBody TaskDto taskDto) {
        Task task = taskDto.getTask();
        if (PojoUtil.task.needUpdateTask(task)) {
            this.taskService.modifyOne(task);
            return ResultUtil.getResult(ResponseCode.ok, "修改任务成功");
        } else
            return ResultUtil.getResult(ResponseCode.ok, "任务无需修改");
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:remove")
    @CheckTaskExist(value = "所选（部分）任务不存在", batch = true)
    @DeleteMapping("/removeBatch")
    @ApiOperation(value = "教师端批量移除任务", notes = TaskApiNotes.removeTaskBatch)
    public JSONObject removeTaskBatch(@RequestBody TaskDto taskDto) {
        this.taskService.removeTaskBatch(taskDto.getTaskIds());
        return ResultUtil.getResult(ResponseCode.ok, "移除所选任务成功");
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:remove")
    @CheckTaskExist("任务不存在")
    @DeleteMapping("/remove")
    @ApiOperation(value = "教师端移除单个任务", notes = TaskApiNotes.removeTask)
    public JSONObject removeTask(@RequestBody TaskDto taskDto) {
        this.taskService.removeTaskBatch(PojoUtil.pojo.convert2TaskIds(taskDto.getTaskId()));
        return ResultUtil.getResult(ResponseCode.ok, "移除任务成功");
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:removeGroup")
    @CheckTaskExist("任务不存在")
    @CheckTaskGroupExist(value = "所选（部分）群组已被移除出任务", batch = true)
    @DeleteMapping("/removeGroupBatch")
    @ApiOperation(value = "教师端移除任务中的多个群组", notes = TaskApiNotes.removeGroupBatch)
    public JSONObject removeGroupBatch(@RequestBody TaskDto taskDto) {
        this.taskService.removeGroupBatch(taskDto.getTaskId(), taskDto.getGroupIds());
        return ResultUtil.getResult(ResponseCode.ok, "移除所选群组成功");
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:removeGroup")
    @CheckTaskExist("任务不存在")
    @CheckTaskGroupExist("群组已被移除出任务")
    @DeleteMapping("/removeGroup")
    @ApiOperation(value = "教师端移除任务中的单个群组", notes = TaskApiNotes.removeGroup)
    public JSONObject removeGroup(@RequestBody TaskDto taskDto) {
        this.taskService.removeGroupBatch(taskDto.getTaskId(), PojoUtil.pojo.convert2GroupIds(taskDto.getGroupId()));
        return ResultUtil.getResult(ResponseCode.ok, "移除群组成功");
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("task:list")
    @SetUserId
    @Paging(DataType.TASK)
    @CheckDataExist("没有查到相关任务信息")
    @PostMapping("/tea")
    @ApiOperation(value = "教师端获取任务列表", notes = TaskApiNotes.tea)
    public JSONObject tea(@RequestBody TaskDto taskDto) {
        PageHelper.startPage(taskDto.getPageNum(), taskDto.getPageSize());
        List<Task> tasks = this.taskService.findTaskTea(taskDto.getTask());
        PageInfo<Task> pageInfo = new PageInfo<>(tasks);
        return ResultUtil.getResult(ResponseCode.ok, "成功返回任务列表", pageInfo);
    }

    @RequiresRoles("teacher")
    @RequiresPermissions("group:list")
    @SetUserId
    @Paging(DataType.GROUP)
    @CheckDataExist("没有查到相关群组信息")
    @PostMapping("/tea/group")
    @ApiOperation(value = "教师端获取任务中的群组列表", notes = TaskApiNotes.taskGroupTea)
    public JSONObject taskGroupTea(@RequestBody GroupDto groupDto) {
        PageHelper.startPage(groupDto.getPageNum(), groupDto.getPageSize());
        List<Group> groups = this.taskService.findTaskGroupTea(groupDto);
        PageInfo<Group> pageInfo = new PageInfo<>(groups);
        return ResultUtil.getResult(ResponseCode.ok, "成功返回群组列表", pageInfo);
    }

    @RequiresRoles("group:list")
    @ApiOperation("获取任务中的群组数")
    @GetMapping("/{taskId}/groupNum")
    public JSONObject getTaskGroupNum(@PathVariable Integer taskId) {
        Integer groupNum = this.taskService.getTaskGroupNum(taskId);
        return ResultUtil.getResult(ResponseCode.ok, "成功返回接收任务的群组数", groupNum);
    }

}
